package com.kaifamiao.service.impl;

import com.kaifamiao.dao.IAccountDao;
import com.kaifamiao.model.Account;
import com.kaifamiao.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lucien-ma
 * @date 2021/8/27
 * @apiNote
 */
@Service
public class IAccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountDao accountDao;

    @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    @Override
    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    @Override
    public void transafer(Account source, Account target) {
        /*
           将 source 的钱 转 100 元 给 target
             省略 查询账户余额
             1. 将 source 的钱 -100
             2. 将 target 的钱 +100
         */
        // 开启事务
        source.setMoney(-100.0);
        accountDao.updateMoney(source);

        int i = 1 / 0;

        target.setMoney(100.0);
        accountDao.updateMoney(target);
        // 提交事务
    }
}
